<?php
class UpdatesController extends AppController {

	var $name = 'Updates';
	var $uses=array('Member');
	function index() {
		if($_FILES){
			if ($_FILES["file"]["type"] == "application/vnd.ms-excel" || $_FILES["file"]["type"] == 		"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" )
		  {
		  if ($_FILES["file"]["error"] > 0)
		    {
		     echo "<script>alert('Return Code:".$_FILES["file"]["error"]."');</script>";
		    }
		  else
		    {	
		    if (file_exists("upload/filedb/" . $_FILES["file"]["name"]))
		      {
		      unlink("upload/filedb/" . $_FILES["file"]["name"]);
		      }
		      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/filedb/" . $_FILES["file"]["name"]);
		      $this->Session->write('filecsv',DOMAIN. "/upload/filedb/" . $_FILES["file"]["name"]);
		       $this->Session->write('filename',$_FILES["file"]["name"]);
		      $this->redirect('dunp');
		    }
		  }
		else
		  {
		  echo "<script>alert('Invalid file');</script>";
		  }
		}
	}
	
	
		function dunp() {
					require_once 'dump_db/PHPExcel/IOFactory.php';
					$insert_count = 0;
					$update_count = 0;
					$errors = array();
					mysql_query("SET NAMES utf8");
					$row = 1;
					$filecsv=$this->Session->read('filecsv');
					$filename=$this->Session->read('filename');
					
					$this->Session->delete('filecsv');
					$this->Session->delete('filename');	
					
					//die();
					if($filecsv=="") {
						$this->redirect('index');
					}
					$objPHPExcel = PHPExcel_IOFactory::load("upload/filedb/".$filename);
					
					$errors = array();
					if($objPHPExcel->getWorksheetIterator()){
						$i=0;
					
						foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
							if($i>0) break;
							//setlocale(LC_ALL, 'ja_JP');
							$worksheetTitle     = $worksheet->getTitle();
							$highestRow         = $worksheet->getHighestRow(); // e.g. 10
							$highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
							$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);	
							$nrColumns = ord($highestColumn) - 64;
								for ($row = 2; $row <= $highestRow; ++ $row) {
										for ($col = 0; $col < $highestColumnIndex; ++ $col) {
											$cell = $worksheet->getCellByColumnAndRow($col, $row);
											$val = $cell->getValue();	
											if($val=='') $val=" ";
											$data[$col]=$val;
											$arr_data[$row-2]=$data;											
										}
							   //Kien tra file co dung dinh dang khong
							
							   //pr(count($data));die();
							   if(count($data)!= 16){
								   	echo "<script>alert('".json_encode('File không đúng định dạng ')."');</script>";
									echo "<script>location.href='".DOMAIN."/members/import'</script>";
								}
							// kiem tra ton tai cua ID cha
								$listIdprent=$this->Member->findByUser($data[5]);								
								if($listIdprent==null){
									$errors[$i] = "Không tìm được cha!";
									continue;
								
								}
								
												
								$arrMember[$row]=array('Member'=>array(
																	'name'=>$data[0],
																	'user'=>$data[1],
																	'pass'=>md5($data[2]),
																	'levels'=>$data[3],
																	'personal_pv'=>$data[4],
																	'parent_id'=>$listIdprent['Member']['id'],
																	'passport'=>$data[7],																	
																	'birthday'=>$data[8],
																	'tax_code'=>$data[9],
																	'deal'=>$data[10],
																	'add'=>$data[11],
																	'email'=>$data[12],
																	'mobile'=>$data[13],
																	'yahoo'=>$data[14],
																	'skype'=>$data[15],
																	'status'=>1,
																	
																	));
								
								
								$this->Member->saveall($arrMember[$row]);
								
								}
								echo "<script>location.href='".System."/members'</script>";
																					
						}
							
						} 
						

	}
}
?>